package com.tangkun.environment.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tangkun.environment.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author tangkun
 * @ClassName: HealthButlerAdapter
 * @Description: 搜索历史列表适配器
 * @date 2016-08-20 上午11:57:36
 */
public class SearchHistoryAdapter extends BaseAdapter {

    private Context context;
    private List<String> listData = new ArrayList<String>();

    /**
     * @param context
     * @param listData
     */
    public SearchHistoryAdapter(Context context, List<String> listData) {
        this.context = context;
        this.listData = listData;
    }

    @Override
    public int getCount() {
        //大于20条只显示前20条
        if (listData.size() > 20) {
            return 20;
        }
        return listData.size();
    }

    @Override
    public Object getItem(int i) {
        return listData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_search_history, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (listData != null) {
            // 内容
            if (!TextUtils.isEmpty(listData.get(position))) {
                holder.tvSearchHistoryContent.setText(listData.get(position));
            } else {
                holder.tvSearchHistoryContent.setText("");
            }
        }
        return convertView;

    }

    static class ViewHolder {
        @BindView(R.id.tv_search_history_content)
        TextView tvSearchHistoryContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
